package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.ItemPageService;
import com.dongdongshop.service.ItemService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DubboService
@RocketMQMessageListener(topic = "dongdongshop-topic",consumerGroup = "dongdongshop_item")
public class ItemPageServiceImpl implements ItemPageService, RocketMQListener<String> {

    @Value("${itempage.outPath}")
    private String outPath;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @DubboReference
    private GoodsService goodsService;

    @DubboReference
    private ItemService itemService;

    @DubboReference
    private ItemCatService itemCatService;

    @Override
    public void itemPage(Long goodsId) {

    }

    @Override
    public void onMessage(String s) {
        //第一步、创建一个 Configuration 对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        FileWriter fileWriter = null;
        try {
            //第四步：加载一个模板，创建一个模板对象。
            Template template = configuration.getTemplate("item.ftl");
            List<Long> objects = JSONObject.parseArray(s,Long.class);
            for (Long goodsId : objects) {
                //根据goodsId查询Goods商品数据，根据ID
                TbGoods goods = goodsService.selectByGoodsId(goodsId);
                TbGoodsDesc goodsDesc = goodsService.getGoodsDescByGoodsId(goodsId);
                List<TbItem> itemList = itemService.selectByGoodsId(goodsId);

                //查询三级
                TbItemCat category1 = itemCatService.getInfoById(goods.getCategory1Id());
                TbItemCat category2 = itemCatService.getInfoById(goods.getCategory2Id());
                TbItemCat category3 = itemCatService.getInfoById(goods.getCategory3Id());

                //第五步：创建一个模板使用的数据集，可以是 pojo 也可以是 map。一般是 Map。
                Map map = new HashMap();
                map.put("goods",goods);
                map.put("goodsDesc",goodsDesc);
                map.put("itemList",itemList);
                map.put("category1",category1.getName());
                map.put("category2",category2.getName());
                map.put("category3",category3.getName());
                //第六步：创建一个 Writer 对象，一般创建一 FileWriter 对象，指定生成的文件名。
                fileWriter = new FileWriter(outPath+goodsId+".html");
                //第七步：调用模板对象的 process 方法输出文件。
                template.process(map,fileWriter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileWriter != null){
                try {
                    //第八步：关闭流
                    fileWriter.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
